#include <stdlib.h>
int temperatureCommand  = B00000011;  // command used to read temperature
int humidityCommand = B00000101;  // command used to read humidity

int clockPin = 8;  // pin used for clock of TEMP
int dataPin  = 9;  // pin used for data of TEMP
int ledPinR = 4; //pin used for RGBL color RED
int ledPinG = 2; //pin used for RGBL color GREEN
int ledPinB = 3; //pin used for RGBL color BLUE
int sound = 5; // pin used for SOUND
int touchSensor = 1; // pin used for Touchsensor
int potentioPin1 = 3; // pin used for Potentiometer/atmosphere-changer
int potentioPin2 = 4; // pin used for Potentiometer/volume-changer
int potentioPin3 = 2; // pin used for Potentiometer/color-changer
//HELP_VARIABLES
int ack;  // track acknowledgment for errors
int val;
float temperature;
float humidity;
int p = 0;
int volume = 0;
int color = 0;
int colorf = 0;
int s = 0;
int touch = 0;
int red = 0;
int blue = 0;
int green = 0;

void changeColor(int red, int green, int blue){
  analogWrite(ledPinR, red);
  analogWrite(ledPinG, green);
  analogWrite(ledPinB, blue);
}

void setup() {                
  Serial.begin(9600);
  pinMode(ledPinR, OUTPUT);
  pinMode(ledPinG, OUTPUT);
  pinMode(ledPinB, OUTPUT);
}

int getData16SHT(int dataPin, int clockPin)
{
  int val;
  // get the MSB (most significant bits)
  pinMode(dataPin, INPUT);
  pinMode(clockPin, OUTPUT);
  val = shiftIn(dataPin, clockPin, 8);
  val *= 256; // this is equivalent to val << 8;
  // send the required ACK
  pinMode(dataPin, OUTPUT);
  digitalWrite(dataPin, HIGH);
  digitalWrite(dataPin, LOW);
  digitalWrite(clockPin, HIGH);
  digitalWrite(clockPin, LOW);
  // get the LSB (less significant bits)
  pinMode(dataPin, INPUT);
  val |= shiftIn(dataPin, clockPin, 8);
  return val;
}

//01:Temp,02:Poty,03:Huminity,04:Sound
void loop() {
  //For Potentiometers
   volume = map (analogRead(potentioPin3),0,1023,0,100);
   color = analogRead(potentioPin2);
   p = analogRead(potentioPin1);
   if (p<204){
     //no Color
      digitalWrite(ledPinR,HIGH);
      digitalWrite(ledPinG,HIGH);
      digitalWrite(ledPinB,HIGH);
   } else if (p>=204 && p<408){
     //RED
      digitalWrite(ledPinR,LOW);
      digitalWrite(ledPinG,HIGH);
      digitalWrite(ledPinB,HIGH);
   } else if(p>=408 && p<612){
     //GREEN
      digitalWrite(ledPinR,HIGH);
      digitalWrite(ledPinG,LOW);
      digitalWrite(ledPinB,HIGH);
   } else if(p>=612 && p<820){
     //BLUE
      digitalWrite(ledPinR,HIGH);
      digitalWrite(ledPinG,HIGH);
      digitalWrite(ledPinB,LOW);
   } else if(p>=820 && p<1024){
     //WHITE
      digitalWrite(ledPinR,LOW);
      digitalWrite(ledPinG,LOW);
      digitalWrite(ledPinB,LOW);
   }
   if (color==0){
     colorf=0;
   } else if (color>0 && color<204){
       colorf= 1000;
   } else if (color>=204 && color<408){
       colorf= 500;
   } else if(color>=408 && color<612){
      colorf= 300;
   } else if(color>=612 && color<820){
      colorf= 200;
   } else if(color>=820 && color<1024){
      colorf= 100;
   }
 
  s = analogRead(sound);
  touch = map (analogRead(touchSensor),0,1023,0,255);
  // for Temperature
  sendCommandSHT(temperatureCommand, dataPin, clockPin);
  waitForResultSHT(dataPin);
  val = getData16SHT(dataPin, clockPin);
  skipCrcSHT(dataPin, clockPin);
  temperature = (float)val * 0.01 - 40;
  // read the humidity
  sendCommandSHT(humidityCommand, dataPin, clockPin);
  waitForResultSHT(dataPin);
  val = getData16SHT(dataPin, clockPin);
  skipCrcSHT(dataPin, clockPin);
  humidity = -4.0 + 0.0405 * val + -0.0000028 * val * val;
  Serial.print("00:");
  Serial.print(temperature);
  Serial.print(",");
  Serial.print("01:");
  Serial.print(p);
  Serial.print(",");
  Serial.print("02:");
  Serial.print(humidity);
  Serial.print(",");
  Serial.print("03:");
  Serial.print(touch);
  Serial.print(",");
  Serial.print("04:");
  Serial.print(s);
  Serial.print(",");
  Serial.print("05:");
  Serial.print(volume);
  Serial.print(",");
  Serial.print("06:");
  Serial.println(colorf);
  
  delay(200);
}

// commands for reading/sending data to a SHTx sensor

// send a command to the SHTx sensor
void sendCommandSHT(int command, int dataPin, int clockPin)
{
  int ack;

  // transmission start
  pinMode(dataPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  digitalWrite(dataPin, HIGH);
  digitalWrite(clockPin, HIGH);
  digitalWrite(dataPin, LOW);
  digitalWrite(clockPin, LOW);
  digitalWrite(clockPin, HIGH);
  digitalWrite(dataPin, HIGH);
  digitalWrite(clockPin, LOW);

  // shift out the command (the 3 MSB are address and must be 000, the last 5 bits are the command)
  shiftOut(dataPin, clockPin, MSBFIRST, command);

  // verify we get the right ACK
  digitalWrite(clockPin, HIGH);
  pinMode(dataPin, INPUT);
  ack = digitalRead(dataPin);
  if (ack != LOW)
    Serial.println("ACK error 0");
  digitalWrite(clockPin, LOW);
  ack = digitalRead(dataPin);
  if (ack != HIGH)
    Serial.println("ACK error 1");
}

// wait for the SHTx answer
void waitForResultSHT(int dataPin)
{
  int ack;

  pinMode(dataPin, INPUT);
  for (int i = 0; i < 100; ++i)
  {
    delay(20);
    ack = digitalRead(dataPin);
    if (ack == LOW)
      break;
  }
  if (ack == HIGH)
    Serial.println("ACK error 2");
}



// skip CRC data from the SHTx sensor
void skipCrcSHT(int dataPin, int clockPin)
{
  pinMode(dataPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  digitalWrite(dataPin, HIGH);
  digitalWrite(clockPin, HIGH);
  digitalWrite(clockPin, LOW);
}
